package com.dshore.messagecenter.dao.warning;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.statistics.MrKPIRecordCurrent;
import com.dshore.messagecenter.model.topicqueue.MdTopic;
import com.dshore.messagecenter.model.warning.MrWarningProcessing;
import com.dshore.messagecenter.model.warning.MrWarningThreshold;

@Repository
public class WarningRecordDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	/*
	 * 拿到阈值表数据
	 */
	public List<MrWarningThreshold> selectThreshold(String timeCode) {
		String sql="SELECT ID,MONITORING_PERIOD,MONITORING_THRESHOLD,PRINCIPAL_ID,WARNING_TYPE,KPI_ID,STATUS,DEL_FLAG,COMPARISON_OPERATORS,OBJ_TYPE,OBJ,UNIT_CODE,CREATED_TIME,MODIFIED_TIME,CREATED_BY_ID,MODIFIED_BY_ID,SEQ FROM T_MR_WARNING_THRESHOLD WHERE DEL_FLAG = 0 AND MONITORING_PERIOD=?";
		RowMapper<MrWarningThreshold> userRowMapper = new BeanPropertyRowMapper<>(MrWarningThreshold.class);
		return jdbcTemplate.query(sql,userRowMapper,timeCode);
	}

	/*
	 * 通知方式名称
	 */
	public String selectWarningTypeName(String code) {
		String sql="SELECT DICT_NAME FROM T_SYS_DICT WHERE DICT_TYPE='inform_way' AND DICT_CODE=?";
		return jdbcTemplate.queryForObject(sql.toString(), String.class,code);
	}
	/*
	 * 记录到处理表
	 */
	public Integer insertProcessing(MrWarningProcessing mrWarningProcessing) {
		String sql="INSERT INTO T_MR_WARNING_PROCESSING(ID,ALARM_ITEM,ALARM_TIME,WARNING_THRESHOLD,COMPARISON_OPERATORS,ACTUAL_NUMERICAL,OBJ_TYPE,OBJ,PRINCIPAL,WARNING_TYPE,STATUS,DEL_FLAG,CREATED_TIME,UNIT) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql.toString(), mrWarningProcessing.getId(),mrWarningProcessing.getAlarmItem(),mrWarningProcessing.getAlarmTime(),mrWarningProcessing.getWarningThreshold(),mrWarningProcessing.getComparisonOperators(),mrWarningProcessing.getActualNumerical(),mrWarningProcessing.getObjType(),mrWarningProcessing.getObj(),mrWarningProcessing.getPrincipal(),mrWarningProcessing.getWarningType(),mrWarningProcessing.getStatus(),mrWarningProcessing.getDelFlag(),mrWarningProcessing.getCreatedTime(),mrWarningProcessing.getUnit());
	}
	/*
	 * 查比较符
	 */
	public String selectSymbol(String code) {
		String sql="SELECT DICT_NAME FROM T_SYS_DICT WHERE DICT_TYPE='threshold_value_range' AND DICT_CODE=?";
		return jdbcTemplate.queryForObject(sql.toString(), String.class,code);
	}
	/*
	 * 查最新的信息
	 */
	public List<MrKPIRecordCurrent> selectMessage(String kpiCode,String objType){
		String sql="SELECT ID,KPI_CODE,KPI_VALUE,OBJ_TYPE,OBJ,CREATED_TIME FROM T_MR_KPI_RECORD_CURRENT WHERE CREATED_TIME =(SELECT MAX(CREATED_TIME) FROM T_MR_KPI_RECORD_CURRENT) AND KPI_CODE=? AND OBJ_TYPE=?";
		RowMapper<MrKPIRecordCurrent> RowMapper = new BeanPropertyRowMapper<>(MrKPIRecordCurrent.class);
		return jdbcTemplate.query(sql.toString(),RowMapper,kpiCode,objType);
	}
	/*
	 * 查阈值时间编码
	 */
	public String selectTimeCode(String dictName) {
		String sql="SELECT DICT_CODE FROM T_SYS_DICT WHERE DICT_TYPE='period_time' AND DICT_NAME=?";
		return jdbcTemplate.queryForObject(sql.toString(), String.class,dictName);
	}
	/**
	 * 查询所有未删除已发布的主题
	 * @param string
	 * @return
	 */
	public List<MdTopic> selectTopic(String status) {
		String sql="SELECT ID,TOPIC_QUEUE_NO,TOPIC_QUEUE_NAME,ACCESS_TYPE,VHOST_NAME,EXCHANGE_NAME,TEMPLATE_ID FROM T_MD_TOPIC WHERE DEL_FLAG=0 AND STATUS=?";
		RowMapper<MdTopic> rowMapper = new BeanPropertyRowMapper<>(MdTopic.class);
		return jdbcTemplate.query(sql,rowMapper,status);
	}
	/*
	 * 拉取队列
	 */
	public List<String> selectTopicAccess(String topicQueueNo) {
		String sql="SELECT ID,QUEUE_NAME FROM T_MD_TOPIC_ACCESS WHERE DEL_FLAG=0 AND TOPIC_QUEUE_NO =?";
		RowMapper<String> rowMapper = new BeanPropertyRowMapper<>(String.class);
		return jdbcTemplate.query(sql, rowMapper,topicQueueNo);
	}
	/*
	 * 发送队列
	 */
	public List<String> selectTopicSend(String topicQueueNo) {
		String sql="SELECT ID,QUEUE_NAME FROM T_MD_TOPIC_SEND WHERE DEL_FLAG=0 AND TOPIC_QUEUE_NO =?";
		RowMapper<String> rowMapper = new BeanPropertyRowMapper<>(String.class);
		return jdbcTemplate.query(sql, rowMapper,topicQueueNo);
	}
	/*
	 * 获取所有队列的最新信息
	 */
	public List<MrKPIRecordCurrent> selectMessage(List<String> accesses, List<String> sends) {
		List<String>list=new ArrayList<>();
		list.addAll(accesses);
		list.addAll(sends);
		Object[]args=list.toArray();
		StringBuffer sql=new StringBuffer("SELECT ID,KPI_CODE,KPI_VALUE,OBJ_TYPE,OBJ FROM T_MR_KPI_RECORD_CURRENT WHERE CREATED_TIME =(SELECT MAX(CREATED_TIME) FROM T_MR_KPI_RECORD_CURRENT) AND OBJ_TYPE='1704' AND OBJ IN (");
		for(int i=0;i<args.length;i++) {
			sql.append("?");
			if(i!=args.length-1) {
				sql.append(",");
			}
		}
		sql.append(")");
		RowMapper<MrKPIRecordCurrent> rowMapper = new BeanPropertyRowMapper<>(MrKPIRecordCurrent.class);
		return jdbcTemplate.query(sql.toString(),rowMapper,args);
	}
	/*
	 * 内存单位
	 */
	public String selectUnitName(String unitCode) {
		String sql="SELECT DICT_NAME FROM T_SYS_DICT WHERE DICT_TYPE='unit' AND DICT_CODE=?";
		return jdbcTemplate.queryForObject(sql,String.class,unitCode);
	}
}
